<template>
  <div>
    <div class="select_top">
      <el-date-picker
        style="width:15%;margin-right:1%"
        v-model="year"
        type="year"
        placeholder="选择年"
        :clearable="false"
        valueFormat="yyyy"
      >
      </el-date-picker>
      <el-button @click="gettestList">筛选</el-button>
    </div>
    <el-table
      ref="filterTable"
      :data="testList"
      v-loading="loadingtt"
      style="width: 92%;font-size:13px;margin: 20px auto;"
    >
      <el-table-column
        type="index"
        label="序号"
      >
      </el-table-column>
      <el-table-column
        prop="hash.aorgname"
        label="考核部门"
      >
      </el-table-column>
      <el-table-column
        prop="hash.time"
        label="填报时间"
      >
      </el-table-column>
      <el-table-column
        prop="hash.statusname"
        label="状态"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="120"
      >
        <template slot-scope="scope">
          <div v-if="belongunit!='3'">
            <el-button
              v-if="scope.row.hash.statusname=='已完成'"
              @click="edit(scope.row)"
              type="primary"
              size="small"
            >
              查看
            </el-button>
            <el-button
              @click="edit(scope.row)"
              v-else-if="scope.row.hash.statusname=='草稿'"
              type="primary"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="edit(scope.row)"
              v-else
              type="primary"
              size="small"
            >
              填报
            </el-button>
          </div>
          <div v-else>
            <el-button
              v-if="scope.row.hash.statusname=='已完成'"
              @click="edit(scope.row)"
              type="primary"
              size="small"
            >
              查看
            </el-button>
            <el-button
              v-else
              size="small"
              @click="cannotsee"
            >
              查看
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title=""
      :visible.sync="showCheck"
      width="90%"
    >
      <el-table
        :data="detailList"
        border
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          label="考核指标"
        >
          <template slot-scope="scope">
            各区镇职能履职
          </template>
        </el-table-column>
        <el-table-column
          prop="hash.borgname"
          label="评议对象"
        >
        </el-table-column>
        <el-table-column
          :renderHeader="foo1"
        >
          <el-table-column labelClassName="f10"
            label="对于城市管理办事处日常工作中涉及
职能未予配合、支持、落实的，每次扣
2分;协调配合过程中未按要求参加城
市管理办事处组织开展的专项整治行
动的，每次扣1分。以上最高扣5分。示例：如扣1分则填写1"
          >
            <template slot-scope="scope">
              — <el-input v-model="scope.row.hash.score3" type="number" min="0" max="5"
                @change="isout"
                v-validate="'required|between:0,5'" name="日常协调配合"
                style="width:80%"
              ></el-input>
              <div style="font-size:11px;color:#e78a42">{{ errors.first('日常协调配合') }}</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          label="问题及建议"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.hash.remark" clearable></el-input>

          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer" v-if="ishowsubmit">
        <el-button @click="saveEdit">暂 存</el-button>
        <el-button type="primary" @click="sumitEdit">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tooltip from '~/components/common/tooltip.vue';

export default {
  data() {
    return {
      showCheck: false,
      loadingtt: true,
      ts: this.newStore(),
      belongunit: localStorage.getItem('belongunit'),
      year: '',
      ishowsubmit: false,
      testList: [],
      detailList: [],
    };
  },
  components: {
    tooltip,
  },
  created() {
    this.year = `${new Date().getFullYear()}`;
    this.gettestList();
  },
  methods: {
    isout(val) {
      for (let i = 0; i < this.detailList.length; i += 1) {
        if (val > 5) {
          this.$message({
            message: '日常协调配合分数不能大于5',
            type: 'error',
          });
          return;
        }
      }
    },
    cannotsee() {
      this.$message({
        message: '该状态下不可查看！',
        type: 'info',
      });
    },
    foo1() {
      return (
          <el-tooltip
          popper-class="tip1"
          effect="dark"
          content="1.办事处发函提请相关区镇协调有关工作
(或专项行动)，相关区镇未按要求派员予
以履职、反馈意见的，每次扣1分;
2.相关区镇未在规定时间完成反馈，整改不
到位，造成重复投诉或苏州市级以上信访平
台投诉的，每次扣2分;
3.相关区镇派员参与专项整治行动时，不服
从指挥，存在不作为、乱作为、慢作为情形
的，每次扣2分"
          placement="bottom"
          >
            <span>日常协调配合<i class="el-icon-question"></i></span>
          </el-tooltip>
      );
    },
    sumitEdit() {
      this.$validator.validateAll().then((result) => {
        if (result) {
          for (let i = 0; i < this.detailList.length; i += 1) {
            if (Number(this.detailList[i].hash.score3) < 0) {
              this.$message({
                message: '请检查，日常协调配合不能小于0',
                type: 'error',
              });
              return false;
            }
          }
          const b = [];
          for (let i = 0; i < this.detailList.length; i += 1) {
            const req = {
              handle: '04A3682CDCA57EBA0DA6C15FB2ECC84B571521205BF7630E125D30625A61C18F',
              score3: this.detailList[i].hash.score3,
              remark: this.detailList[i].hash.remark,
              status: '1',
              taskid: this.detailList[i].hash.id,

            };
            b.push(this.spost(this.ts, '/mainservlet', req).then(() => {
              this.showCheck = false;
            }));
          }
          Promise.all(b).then(() => {
            this.$message({
              message: '提交成功！',
              type: 'success',
            });
            this.gettestList();
          });
        } else {
          this.$message.error('内容不能为空或不符合规则！');
          return false;
        }
      });
    },
    saveEdit() {
      for (let i = 0; i < this.detailList.length; i += 1) {
        if (Number(this.detailList[i].hash.score3) < 0) {
          this.$message({
            message: '请检查，日常协调配合不能小于0',
            type: 'error',
          });
          return false;
        }
      }
      const a = [];
      for (let i = 0; i < this.detailList.length; i += 1) {
        const req = {
          handle: '04A3682CDCA57EBA0DA6C15FB2ECC84B571521205BF7630E125D30625A61C18F',
          score3: this.detailList[i].hash.score3,
          remark: this.detailList[i].hash.remark,
          status: '0',
          taskid: this.detailList[i].hash.id,

        };
        a.push(this.spost(this.ts, '/mainservlet', req).then(() => {
          this.showCheck = false;
        }));
      }
      Promise.all(a).then(() => {
        this.$message({
          message: '暂存成功！',
          type: 'success',
        }); this.gettestList();
      });
    },
    edit(item) {
      if (item.hash.statusname == '已完成') {
        this.ishowsubmit = false;
      } else {
        this.ishowsubmit = true;
      }
      this.showCheck = true;
      const req = {
        handle: '04A3682CDCA57EBA0DA6C15FB2ECC84BECD88FF3EF8A7E3E23BEBB2A1CA3F337BFF0B9210751F692',
        aorgid: item.hash.aorgid,
        time: item.hash.time,
        stype: '2',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.detailList = ts.d.data.operation1;
      });
    },
    gettestList() {
      const req2 = {
        handle: '04A3682CDCA57EBA0DA6C15FB2ECC84BECD88FF3EF8A7E3EBDF7AE0978A2036F',
        year: this.year,
        stype: '2',
      };
      this.spost(this.ts, '/mainservlet', req2).then((ts) => {
        this.testList = ts.d.data.operation1;
        this.loadingtt = false;
      });
    },
  },
};
</script>

<style lang="stylus">
.tip1
  width 300px;
  line-height 20px
.f10
  font-size 12px
.el-table .cell
  text-align center
.el-table th>.cell
  text-align center
.el-input--suffix .el-input__inner
  font-size 13px
.el-button
  font-size 13px
.el-select
  width 14%
  margin-right 1%
.select_top
  width 92%
  margin 20px auto
.el-col
  padding: 20px 10px;
</style>
